<?php
/**
 * 内部用户登录，如服务台、工作人员处理事件
 **/
class adminWorker extends SDO_Controller
{
	private $params;
	
	public function __construct()
	{
		parent::__construct();
		header("Content-type:text/html;charset=utf-8");		
		$this->load->helper("ip");
		if (!ip_helper::is_company())
			die("非公司网络，不允许登录！！！");
		if (!$_SESSION['username'])
			redirect('/welcome');
		$this->load->model("workers");
		$this->load->model("groups");
		$this->params = array_merge($_GET, $_POST);
	}

	public function index()
	{
		redirect('/adminWorker/workerList');
	}
	/**
	 * 管理员列表页
	 **/
	public function workerList()
	{
		$this->load->view('adminWorker/workerList', $data);
	}
	
	public function NewWorker()
	{	    
		$workerId = !isset($this->params['worker_id']) ? 0:intval($this->params['worker_id']);
		$data = array();
		$data['workerId'] = $workerId;
		if (!$workerId) {
			$data['worker']	= array('worker_name'=>'', 'worker_groupid'=>0, 'is_charger'=>0);
		} else {
			$data['worker'] = $this->workers->getWorkerById($workerId);
		}
	    $data['group_list']	= $this->groups->getAllGroups();

		$this->load->view('adminWorker/NewWorker', $data);
	}
	
	public function modifyWorker()
	{
		$workerId = !isset($this->params['worker_id']) ? 0:intval($this->params['worker_id']);
		
		if ($workerId) {
			$row = $this->workers->getWorkerById($workerId);
			if (!$row) {
				$workerId = 0;
			}
		}
		$row = array(
			'worker_name'=> trim($this->params['worker_name']),
			'worker_groupid'=> intval($this->params['worker_groupid']),
			'is_charger'=> intval($this->params['is_charger']),
		);
		if (!$workerId) {
			if(!($workerId = $this->workers->addWorker(0, 0, $row))){
				user_helper::tips("添加管理员失败", "/adminWorker/NewWorker/", $this);
			}
		} else {
			if (!$this->workers->modifyWorkerById($workerId, $row)) {
				user_helper::tips("更新管理员信息失败", "/adminWorker/NewWorker/?worker_id=".$workerId, $this);
			}
		}
		redirect("/adminWorker/workerList/", 'refresh');
	}
	
	public function RemoveWorker()
	{
		if(!isset($this->params['worker_id'])) {
			user_helper::tips("不存在的管理员信息", "/adminWorker/workerList/", $this);
		}
		
		$workerId = intval($this->params['worker_id']);
		if (!($row=$this->workers->getWorkerById($workerId))) {
			user_helper::tips("不存在的管理员信息", "/adminWorker/workerList/", $this);
		}
		if (!$this->workers->deleteWorkerById($workerId)) {
			user_helper::tips("删除管理员失败", "/adminWorker/workerList/", $this);
		}
		user_helper::tips("已成功删除管理员({$row['worker_name']})", "/adminWorker/workerList/", $this);
	}
	/**
	 * 事件列表数据查询
	 **/
	public function adminRequestData()
	{
		$page		= !isset($this->params['page']) ? 1 : intval($this->params['page']);
		$rows		= !isset($this->params['rows']) ? 10 : intval($this->params['rows']);
		$sidx		= !isset($this->params['sidx']) || !$this->params['sidx'] ? 'worker_id' : $this->params['sidx'];
		$sord		= !isset($this->params['sord']) || !$this->params['sord'] ? 'DESC' : $this->params['sord'];
		$offset		= ($page-1)*$rows;
		$where = array();
		
		$count = $this->workers->getWorkerCount($where);

		$data = array();
        if ($count) {
        	$data = $this->workers->getWorkerList($where, $offset, $rows, $sidx, $sord);
        	$this->_parseWorker($data);
        }

		$arr = array();
		$arr['total'] = ceil($count/$rows);
		$arr['records'] = $count;
		$arr['page'] = $page;
		$arr['rows'] = $data;
		echo json_encode($arr);
	}

	private function _parseWorker(&$events)
	{
	    if (!$events) {
	    	return ;
	    }
	    $data = $this->groups->getAllGroups();
	    $groupList = options_helper::fieldDatas($data, 'group_id', 'row');
	    foreach($events as $k=>$row) {
	    	$row['group_name'] = isset($groupList[$row['worker_groupid']]) ? $groupList[$row['worker_groupid']]['group_name'] : '未定义';
	        $row['charger_name'] = $row['is_charger'] ? '负责人':'普通成员';
	        $row['delete_worker'] = $row['worker_id'];
	    	$events[$k] = $row;
	    }
	}

}
